#include "fileutils.h"
#include <QFile>
#include <QJsonDocument>
#include <QJsonArray>
#include <QJsonObject>
#include <QTextStream>
#include <QDebug>
#include <QVector>
#include <QString>
#include <limits> // 包含limits以使用无穷大

QMap<QString, QVector<double>> readJsonFile(const QString &filePath) {
    QMap<QString, QVector<double>> dataMap;

    // 打开文件
    QFile file(filePath);
    if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) {
        qWarning() << "无法打开文件:" << filePath;
        return dataMap;
    }

    // 读取文件内容
    QByteArray fileContent = file.readAll();
    file.close();

    // 解析JSON文档
    QJsonDocument doc = QJsonDocument::fromJson(fileContent);
    if (!doc.isArray()) {
        qWarning() << "JSON 格式错误";
        return dataMap;
    }

    // 处理JSON数组
    QJsonArray jsonArray = doc.array();
    for (const QJsonValue &value : jsonArray) {
        QJsonObject obj = value.toObject();
        QString name = obj.value("标注名称").toString();
        double longitude = obj.value("经度").toDouble();
        double latitude = obj.value("纬度").toDouble();

        // 将数据存储到字典中
        dataMap[name] = QVector<double>{longitude, latitude};
    }

    return dataMap;
}

QVector<QVector<QString>> readCsvFile(const QString &filePath) {
    QVector<QVector<QString>> data;
    QFile file(filePath);

    if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) {
        qWarning() << "无法打开文件:" << filePath;
        return data;
    }

    QTextStream stream(&file);
    while (!stream.atEnd()) {
        QString line = stream.readLine();
        QStringList fields = line.split(',');

        QVector<QString> row;
        for (const QString &field : fields) {
            row.append(field);
        }
        data.append(row);
    }

    file.close();
    return data;
}

QStringList extractHeaders(const QString &filePath) {
    QStringList headers;
    QFile file(filePath);

    if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) {
        qWarning() << "无法打开文件:" << filePath;
        return headers;
    }

    QTextStream stream(&file);
    bool isFirstLine = true;

    while (!stream.atEnd()) {
        QString line = stream.readLine();
        QStringList fields = line.split(',');

        if (isFirstLine) {
            // 跳过第一行的第一个元素（行标题），提取其余元素
            if (fields.size() > 1) {
                headers = fields.mid(1);  // 提取除第一个元素外的所有标题
            }
            isFirstLine = false;
        }
    }

    file.close();
    return headers;
}


QVector<QVector<double>> convertToDistanceMatrix(const QVector<QVector<QString>> &csvData) {
    int n = csvData.size() - 1;  // 排除首行地名
    QVector<QVector<double>> distanceMatrix(n, QVector<double>(n, 0));

    for (int i = 1; i <= n; ++i) {
        for (int j = 1; j <= n; ++j) {
            double value = csvData[i][j].toDouble();
            if (i == j) {
                distanceMatrix[i - 1][j - 1] = 0; // 对角线保持为0
            } else if (value == 0) {
                distanceMatrix[i - 1][j - 1] = std::numeric_limits<double>::infinity(); // 无法到达的情况
            } else {
                distanceMatrix[i - 1][j - 1] = value; // 实际距离
            }
        }
    }

    return distanceMatrix;
}
QVector<QVector<double>> convertToInterestMatrix(const QVector<QVector<double>> &distanceMatrix) {
    int n = distanceMatrix.size();  // 排除首行地名
    QVector<QVector<double>> InterestMatrix(n-30, QVector<double>(n-30, 0));

    for (int i = 30; i <n; ++i) {
        for (int j = 30; j <n; ++j) {
            InterestMatrix[i-30][j-30]=distanceMatrix[i][j];
        }
    }

    return InterestMatrix;
}
QMap<QString, QString> loadJsonIntro(const QString &filePath)
{
    QMap<QString, QString> resultMap;
    QFile file(filePath);
    if (!file.open(QIODevice::ReadOnly)) {
        qWarning("Could not open the JSON file!");

        return resultMap;
    }

    QByteArray jsonData = file.readAll();
    QJsonDocument doc = QJsonDocument::fromJson(jsonData);
    QJsonObject jsonObj = doc.object();

    for (auto it = jsonObj.begin(); it != jsonObj.end(); ++it) {
        QString key = it.key();
        QString value = it.value().toString();
        resultMap.insert(key, value);
    }

    file.close();
    return resultMap;
}
QVector<QPointF> initializeGeoVector(const QStringList &names, const QMap<QString, QVector<double>> &geoData) {
    QVector<QPointF> geoVector;
    for (const QString &name : names) {
        if (geoData.contains(name)) {
            const QVector<double> &coords = geoData[name];
            if (coords.size() == 2) {
                geoVector.append(QPointF(coords[0], coords[1]));
            }
        }
    }
    return geoVector;
}
